//
//  SecondViewController.m
//  DogLocate
//
//  Created by mac zou on 11-5-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SecondViewController.h"
#import "InfoMgr.h"
#import "WalkInfo.h"
#import "extev.h"
#import "InfoCell.h"
#import "FirstVC.h"
#import "SecondVC.h"
#import "ThirdVC.h"
#import "DogLocateAppDelegate.h"

@implementation SecondViewController
@synthesize receivedInfo;
@synthesize dataArray;


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor = [UIColor blackColor];
	self.navigationItem.title = @"Rides";
	
	UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:@"Edit" 
															style:UIBarButtonItemStylePlain
														   target:self
														   action:@selector(editCell)];
	self.navigationItem.rightBarButtonItem = edit;
	[edit release];
	
	EDIT = NO;
}


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:YES];
	
	//[infoMgr loadDataList];
	self.dataArray = [infoMgr getData];

	table = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, 300, 380) style:UITableViewStyleGrouped];	
	table.delegate = self;
	table.dataSource = self;
	table.backgroundColor = [UIColor clearColor];
	table.separatorColor = [UIColor colorWithRed:0.14 green:0.14 blue:0.14 alpha:1.0f];
	table.rowHeight = 70;
	[self.view addSubview:table];
	[table release];
	
}

-(void)viewDidDisappear:(BOOL)animated {

	[super viewDidDisappear:YES];
	
	[table removeFromSuperview];
}


#pragma mark -Action 

-(void)editCell {
	if (EDIT == NO) {
		table.editing = YES;
		//[table beginUpdates];
		EDIT = YES;

	}else {
		table.editing = NO;
		//[table beginUpdates];
		EDIT = NO;
	}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/





/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	return [self.dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    InfoCell *cell = (InfoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[InfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
//	UIView *v = [[UIView alloc]initWithFrame:cell.frame];
//	v.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1.0f];
//	cell.backgroundView = v;
//	[v release];
	cell.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1.0f];
    
	WalkInfo *info = [self.dataArray objectAtIndex:indexPath.row];
	cell.date.text = [info.walkDate substringToIndex:10];
	cell.distance.text = info.distance;
	
	int seconds = [info.walkCount intValue] % 60;
    int minutes = ([info.walkCount intValue] / 60) % 60;
    int hours = [info.walkCount intValue] / 3600;
	cell.time.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes,seconds];
	
	//NSLog(@"walk city is : %@",info.walkCity);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WalkInfo *info = [self.dataArray objectAtIndex:indexPath.row];
	
	UITabBarController *tab = [[UITabBarController alloc]init];
	FirstVC *first = [[FirstVC alloc]initWithDate:info.walkDate 
											 city:info.walkCity 
										 distance:info.distance
											 time:info.walkCount
											speed:info.avgSpeed];
	first.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMore tag:1];

	SecondVC *second = [[SecondVC alloc]initWithLocationArray:info.locateArray];
	second.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:2];
	
	
	tab.viewControllers = [NSArray arrayWithObjects:first,second,nil];
	
	tab.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:tab animated:YES];

	
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		[self.dataArray removeObjectAtIndex:indexPath.row];
		[infoMgr saveData:self.dataArray];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
		[table reloadData];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

